package com.xingkeduo.business.transformation.json;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.xingkeduo.pojo.table.CraftsmanBackTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

/**
 * 一线员工离职信息封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:27:05
 * @description
 */
@Component
public class CraftsmanBackJTO extends BaseJson {
	public static CraftsmanBackTO createTableForNew(String json) throws Throwable {
		CraftsmanBackTO table = new CraftsmanBackTO();
		JsonNode root = validJson(json);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");

		table.setCraftsmanId(validPrimary(root, "craftsmanId", YesNoEnum.NO))
				.setReason(validReason(root, YesNoEnum.NO));

		return table;
	}

	public static String validReason(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "reason";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > 100)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}
}
